#include "json.h"

std::string to_string(const Json::Value &rValue)
{
    std::unique_ptr<Json::StreamWriter> writer(jsonBuilderInst::instance().writerBuilder.newStreamWriter());
    std::ostringstream oss;
    writer->write(rValue, &oss);
    return oss.str();
}

Json::Value Utils::to_json(const std::string &result)
{
    Json::Value rValue;
    std::unique_ptr<Json::CharReader> reader(jsonBuilderInst::instance().readerBuilder.newCharReader());
    std::string err;
    bool parsingSuccessful = reader->parse(result.c_str(), result.c_str() + result.size(), &rValue, &err);
    if (!parsingSuccessful)
    {
        std::cout << "[RedisClientManager]  Failed to parse JSON: " << err << std::endl;
        return Json::Value();
    }
    return rValue;
}

// int main(int argc, char const *argv[])
// {
//     Json::Value rJson;
//     rJson["1"] = 1;
//     rJson["2"] = "2";
//     rJson["3"] = 3.0;

//     int a;
//     std::string b;
//     float c;
//     load_val(rJson, "1", a);
//     load_val(rJson, "2", b);
//     load_val(rJson, "3", c);
//     std::cout << a << std::endl << b << std::endl << c << std::endl;
//     return 0;
// }